package com.kgar.repository;

import com.kgar.dto.SearchResult;
import com.kgar.entity.graph.node.JobPositionNode;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public interface JobPositionRepository extends Neo4jRepository<JobPositionNode, Long> {


    @Query(
            "match (n:JobPosition) where n.jobPositionId = $jobPositionId return n"
    )
    JobPositionNode findByJobId(Long jobPositionId);

    // 分页查询方法
    @Query("""
            MATCH (e:教育背景)<-[]-(j:职位)-[]->(p:公司)\s
            WHERE j.description CONTAINS $keys OR\s
                  j.title CONTAINS $keys OR\s
                  j.tag CONTAINS $keys OR\s
                  e.educationName CONTAINS $keys OR\s
                  p.companyName CONTAINS $keys OR\s
                  p.address CONTAINS $keys \s
            with j,p ,e ,rand() as r
            RETURN j AS jobPosition, p AS company,e AS education
            ORDER BY jobPosition[$3],r
            SKIP $pageNum * $pageSize
            LIMIT $pageSize""")
    List<SearchResult> searchWithKeys(String keys, int pageNum, int pageSize, String orderBy);


    @Query("""
            MATCH (e:教育背景)<-[]-(j:职位)-[]->(p:公司)\s
            WHERE j.description CONTAINS $keys OR\s
                  j.title CONTAINS $keys OR\s
                  j.tag CONTAINS $keys OR\s
                  e.educationName CONTAINS $keys OR\s
                  p.companyName CONTAINS $keys OR\s
                  p.address CONTAINS $keys \s
            RETURN count(j)""")
    int searchWithKeysTotal(String keys);


    @Query("""
    UNWIND $ids as id
    MATCH (e:教育背景)<-[]-(j:职位)-[]->(p:公司)
    WHERE j.jobPositionId = id
    RETURN DISTINCT j AS jobPosition, p AS company,e AS education
    """)
    List<SearchResult> findJobAndCompanyByIds(List<Long> ids);


}
